/*
 * @Author: 姚杰鸿 adolphus520@163.com
 * @Date: 2024-07-26 10:09:19
 * @LastEditors: 姚杰鸿
 * @LastEditTime: 2025-01-23 16:09:06
 * @Description: file content
 */
import {
  IsValidUserAccountApi,
  IsValidRoleCodeApi,
  IsValidDepartmentCodeApi,
} from "@/apis/home";
import { checkPasswordRule } from "@/lib/passwordCheck";

/**
 * 通过接口检查用户账号是否有效
 */
export const validateUserAccount = (
  rule: any,
  id: any,
  value: any,
  callback: any
) => {
  if (!value) {
    callback(new Error("账号不能为空"));
    return;
  }
  IsValidUserAccountApi(id, value)
    .then((res) => {
      callback();
    })
    .catch((err) => {
      if (err.response && err.response.data && err.response.data.msg)
        if (err.response.data.msg.indexOf("is valid") > -1)
          callback(new Error("用户账号已经被占用"));
        else callback(new Error(err.response.data.msg));
      else callback(new Error("判断用户账号的有效性出现错误！"));
    });
};

export const validateUserPassword = (
  rule: any,
  account: any,
  value: any,
  callback: any
) => {
  if (!value || value === "") {
    callback(new Error("请输入密码"));
    return;
  }
  const result: string = checkPasswordRule(value, account);
  if (result === "校验通过") {
    callback();
  } else {
    callback(new Error(result));
  }
};

/**
 * 通过接口检查角色的Code是否有效
 */
export const validateRoleCode = (
  rule: any,
  id: any,
  value: any,
  callback: any
) => {
  if (!value) {
    callback(new Error("角色编码不能为空"));
    return;
  }
  IsValidRoleCodeApi(id, value)
    .then((res) => {
      callback();
    })
    .catch((err) => {
      if (err.response && err.response.data && err.response.data.msg)
        if (err.response.data.msg.indexOf("is valid") > -1)
          callback(new Error("角色编码已经被占用"));
        else callback(new Error(err.response.data.msg));
      else callback(new Error("判断角色编码的有效性出现错误！"));
    });
};

/**
 * 通过接口检查资源的Code是否有效
 */
export const validateIntfCode = (
  rule: any,
  id: any,
  value: any,
  callback: any
) => {
  if (!value) {
    callback(new Error("资源编码不能为空"));
    return;
  }
  IsValidRoleCodeApi(id, value)
    .then((res) => {
      callback();
    })
    .catch((err) => {
      if (err.response && err.response.data && err.response.data.msg)
        if (err.response.data.msg.indexOf("is valid") > -1)
          callback(new Error("资源编码已经被占用"));
        else callback(new Error(err.response.data.msg));
      else callback(new Error("判断资源编码的有效性出现错误！"));
    });
};

/**
 * 通过接口检查部门的Code是否有效
 */
export const validateDepartmentCode = (
  rule: any,
  id: any,
  value: any,
  callback: any
) => {
  if (!value) {
    callback(new Error("部门编码不能为空"));
    return;
  }
  IsValidDepartmentCodeApi(id, value)
    .then((res) => {
      callback();
    })
    .catch((err) => {
      if (err.response && err.response.data && err.response.data.msg)
        if (err.response.data.msg.indexOf("is valid") > -1)
          callback(new Error("部门编码已经被占用"));
        else callback(new Error(err.response.data.msg));
      else callback(new Error("判断部门编码的有效性出现错误！"));
    });
};
